//脚本集合：actuatorGroup
var dowithActuatorGroup=function(host,active,node,actuators)
{
	for (var i=0;i<node.childNodes.length;i++)
	{
		if (node.childNodes[i].nodeType!=1)
		{
			continue;
		}
		var actuatorNode=node.childNodes[i];
		var actuatorLoader=OS_Actuator.getInstance(actuatorNode.nodeName);
		if (!!actuatorLoader) actuatorLoader.init(host,active,actuatorNode,actuators);
	}
}
OS_Actuator.list.put("actuatorGroup",dowithActuatorGroup);

//playeffect脚本
var dowithPlayEffect=function(host,active,node,actuators)
{
	var sourceid=node.getElementsByTagName("source")[0].childNodes[0].nodeValue;
	var targetid=node.getElementsByTagName("target")[0].childNodes[0].nodeValue;
	var target=null;
	if (targetid=="self") target=active;
	else target=OS_Resource.resource.get(targetid);
	var func=function(){
		var effect_temp=OS_Resource.resource.get(sourceid);
		position_y=target.currentPosition.y/host.config.squareheight;
		position_x=target.currentPosition.x/host.config.squarewidth;
		host.status.activeInterface.playEffect(effect_temp,position_y,position_x);
	};
	actuators.push(func);
}
OS_Actuator.list.put("playEffect",dowithPlayEffect);

//move操作
var dowithMove=function(host,active,node,actuators)
{
	//确定方向
	var direction=node.getAttribute("direction");
	directions={up:0,right:1,down:2,left:3}
	var faceTo=directions[direction];
	//确定目标
	var targetid=node.getElementsByTagName("target")[0].childNodes[0].nodeValue;
	var target=null;
	if (targetid=="self")
	{
		target=active;
	}
	else if (targetid=="lead")
	{
		target=host.status.leadingRole;
	}
	else
	{
		target=OS_Resource.resource.get(targetid);
	}
	var func=function()
	{
		target.faceto(faceTo);
		target.forward(undefined,"resumeactive",function(){
				active.resume();//恢复脚本的执行
			});
		active.pause();//阻塞脚本队列的执行
	};
	actuators.push(func);
}
OS_Actuator.list.put("move",dowithMove);

//talk操作
var dowithTalk=function(host,active,node,actuators)
{
	if (!!node.getElementsByTagName("talker")[0])
	{
		var talker=node.getElementsByTagName("talker")[0].childNodes[0].nodeValue;
		var talkerColor=node.getElementsByTagName("talker")[0].getAttribute("color");
	}
	var talkerPortrait=null;
	if (!!node.getElementsByTagName("portrait")[0])
	{
		talkerPortrait={};
		talkerPortrait.image=OS_Resource.resource.get(node.getElementsByTagName("portrait")[0].getElementsByTagName("source")[0].childNodes[0].nodeValue).getImage();
		talkerPortrait.position=node.getElementsByTagName("portrait")[0].getElementsByTagName("position")[0].childNodes[0].nodeValue;
		talkerPortrait.height=320;
	}
	var wordlist=node.getElementsByTagName("word");
	var words=[];
	for (var i=0;i<wordlist.length;i++)
	{
		words.push(wordlist[i].childNodes[0].nodeValue);
	}
	var func=function()
	{
		var mydirections=[2,3,0,1];
		var directionTemp=active.direction;
		var wordsFinal=[];
		active.direction=mydirections[host.status.leadingRole.direction];
		for (var i=0;i<words.length;i++)
		{
			var textTemp=words[i];
			var vs=textTemp.indexOf("{=");
			while (vs>-1)
			{
				var ve=textTemp.indexOf("}",vs);
				textTemp=textTemp.slice(0,vs)+host.status.V.get(textTemp.slice(vs+2,ve))+textTemp.slice(ve+1);
				var vs=textTemp.indexOf("{=");
			}
			if (!!talker)
			{
				wordsFinal.push(talker+"："+textTemp);
			}
			else
			{
				wordsFinal.push(textTemp);
			}
		}
		var talk={
			words:wordsFinal,
			talker:talker,
			talkerColor:talkerColor,
			talkerPortrait:talkerPortrait,
			callback:function(){
				active.direction=directionTemp;
				active.resume();
				host.status.activeInterface.resume();
			}
		};
		var dialog=new OS_TalkDialog(host,host.status.activeInterface,host.status.activeInterface.getWinRes(),talk);
		dialog.open();
		host.status.activeInterface.pause();
		active.pause();
	};
	actuators.push(func);
}
OS_Actuator.list.put("talk",dowithTalk);

//delay操作
var dowithDelay=function(host,active,node,actuators)
{
	var time=Number(node.getAttribute("time"));
	func=function()
	{
		setTimeout(function(){
			active.resume()
		},time);
		active.pause();
	}
	actuators.push(func);
}
OS_Actuator.list.put("delay",dowithDelay);

//select操作
var dowithSelect=function(host,active,node,actuators)
{
	var key=node.getAttribute("key");
	var text=node.getElementsByTagName("text")[0].childNodes[0].nodeValue;
	var options=[];
	var optionNodes=node.getElementsByTagName("option");
	for (var i=0;i<optionNodes.length;i++)
	{
		var optionNode=optionNodes[i];
		var optionText=optionNode.getElementsByTagName("text")[0].childNodes[0].nodeValue;
		var optionValue=optionNode.getAttribute("value");
		var option={text:optionText,value:optionValue};
		options.push(option);
	}
	var func=function()
	{
		var mydirections=[2,3,0,1];
		var directionTemp=active.direction;
		active.direction=mydirections[host.status.leadingRole.direction];
		var select={
			text:text,
			key:key,
			options:options,
			callback:function()
			{
				active.direction=directionTemp;
				active.resume();
				host.status.activeInterface.resume();
			}
		};
		var dialog=new OS_SelectDialog(host,host.status.activeInterface,host.status.activeInterface.getWinRes(),select);
		dialog.open();
		host.status.activeInterface.pause();
		active.pause();
	}
	actuators.push(func);
}
OS_Actuator.list.put("select",dowithSelect);

//if操作
var dowithIf=function(host,active,node,actuators)
{
	var thenIndex=0;
	var elseIndex=0;
	var nextIndex=0;
	var condition=null;
	var conditionNode=node.getElementsByTagName("condition")[0];
	for (var i=0;i<conditionNode.childNodes.length;i++)
	{
		if (conditionNode.childNodes[i].nodeType!=1) continue;
		condition=OS_Condition.getInstance(host,active,conditionNode.childNodes[i]);
		break;
	}
	var funcIf=function()
	{
		if (condition.check()) active.goto(thenIndex);
		else active.goto(elseIndex);
	}
	var funcEndIf=function()
	{
		active.goto(nextIndex);
	}
	actuators.push(funcIf);
	thenIndex=actuators.length;
	var thenNode=node.getElementsByTagName("then")[0];
	for (var i=0;i<thenNode.childNodes.length;i++)
	{
		if (thenNode.childNodes[i].nodeType!=1) continue;
		var actuatorNode=thenNode.childNodes[i];
		var actuatorLoader=OS_Actuator.getInstance(actuatorNode.nodeName);
		if (!!actuatorLoader) actuatorLoader.init(host,active,actuatorNode,actuators);
		actuators.push(funcEndIf);
		break;
	}
	elseIndex=actuators.length;
	for (var i=0;i<node.childNodes.length;i++)
	{
		if (node.childNodes[i].nodeType!=1) continue;
		if (node.childNodes[i].nodeName=="else")
		var elseNode=node.childNodes[i];
	}
	if (!!elseNode)
	{
		for (var i=0;i<elseNode.childNodes.length;i++)
		{
			if (elseNode.childNodes[i].nodeType!=1) continue;
			var actuatorNode=elseNode.childNodes[i];
			var actuatorLoader=OS_Actuator.getInstance(actuatorNode.nodeName);
			if (!!actuatorLoader) actuatorLoader.init(host,active,actuatorNode,actuators);
			actuators.push(funcEndIf);
			break;
		}
	}
	nextIndex=actuators.length;
}
OS_Actuator.list.put("if",dowithIf);

//setVariable操作
var dowithSetVariable=function(host,active,node,actuators)
{
	var key=node.getAttribute("key");
	var value=node.getAttribute("value");
	var func=function()
	{
		host.status.V.put(key,value);
	}
	actuators.push(func);
}
OS_Actuator.list.put("setVariable",dowithSetVariable);

//fight操作
var dowithFight=function(host,active,node,actuators)
{
	var battleid=node.childNodes[0].nodeValue;
	var func=function()
	{
		host.status.activeInterface.pause();
		active.pause();
		var battle=OS_Resource.resource.get(battleid);
		host.loopEndHooks.push(function(){
			battle.callback=function()
			{
				active.resume();
			}
			battle.show();
		});
	}
	actuators.push(func);
}
OS_Actuator.list.put("fight",dowithFight);

//while操作
var dowithWhile=function(host,active,node,actuators)
{
	var conditionIndex=0;
	var doIndex=0;
	var nextIndex=0;
	var condition=null;
	var conditionNode=node.getElementsByTagName("condition")[0];
	for (var i=0;i<conditionNode.childNodes.length;i++)
	{
		if (conditionNode.childNodes[i].nodeType!=1) continue;
		condition=OS_Condition.getInstance(host,active,conditionNode.childNodes[i]);
		break;
	}
	var funcCondition=function()
	{
		if (condition.check()) active.goto(doIndex);
		else active.goto(nextIndex);
	}
	var funcEndDo=function()
	{
		active.goto(conditionIndex);
	}
	var conditionIndex=actuators.length;
	actuators.push(funcCondition);
	var doIndex=actuators.length;
	var doNode=node.getElementsByTagName("do")[0];
	for (var i=0;i<doNode.childNodes.length;i++)
	{
		if (doNode.childNodes[i].nodeType!=1) continue;
		var actuatorNode=doNode.childNodes[i];
		var actuatorLoader=OS_Actuator.getInstance(actuatorNode.nodeName);
		if (!!actuatorLoader) actuatorLoader.init(host,active,actuatorNode,actuators);
		actuators.push(funcEndDo);
		break;
	}
	nextIndex=actuators.length;
}
OS_Actuator.list.put("while",dowithWhile);

//跳转脚本
var dowithJump=function(host,active,node,actuators)
{
	var sceneid=node.getElementsByTagName("toscene")[0].childNodes[0].nodeValue;
	var positionx=Number(node.getElementsByTagName("position")[0].getElementsByTagName("x")[0].childNodes[0].nodeValue);
	var positiony=Number(node.getElementsByTagName("position")[0].getElementsByTagName("y")[0].childNodes[0].nodeValue);
	var func=function()
	{
		host.changeInterface(sceneid,"scene");
		host.status.leadingRole.currentPosition.x=positionx*host.config.squarewidth;
		host.status.leadingRole.currentPosition.y=positiony*host.config.squarewidth;
		host.status.leadingRole.distanceFromLastSquare=0;
		host.loopStartHooks.push(function(){host.status.leadingRole.occupy();});
	}
	actuators.push(func);
}
OS_Actuator.list.put("jump",dowithJump);

//渐变脚本
var dowithGradual=function(host,active,node,actuators)
{
	var red=Number(node.getAttribute("r"));
	var green=Number(node.getAttribute("g"));
	var blue=Number(node.getAttribute("b"));
	var alpha=Number(node.getAttribute("a"));
	var duration=Number(node.getAttribute("d"));
	var func=function()
	{
		var gradualTemp={};
		gradualTemp.startTime=(new Date()).getTime();
		gradualTemp.duration=duration;
		gradualTemp.fromR=host.status.activeInterface.filter.r;
		gradualTemp.fromG=host.status.activeInterface.filter.g;
		gradualTemp.fromB=host.status.activeInterface.filter.b;
		gradualTemp.fromA=host.status.activeInterface.filter.a;
		gradualTemp.toR=red;
		gradualTemp.toG=green;
		gradualTemp.toB=blue;
		gradualTemp.toA=alpha;
		gradualTemp.callback=function()
		{
			active.resume();
			host.status.activeInterface.resume();
		}
		host.status.activeInterface.gradual=gradualTemp;
		host.status.activeInterface.pause();
		active.pause();
	}
	actuators.push(func);
}
OS_Actuator.list.put("gradual",dowithGradual);

//插图脚本
var dowithIllustration=function(host,active,node,actuators)
{
	var dowhat=node.getElementsByTagName("do")[0].textContent;
	if (dowhat=="clear")
	{
		var func=function()
		{
			host.status.activeInterface.illustration=null;
		}
	}
	else if (dowhat=="draw")
	{
		var source=node.getElementsByTagName("source")[0].textContent;
		var func=function()
		{
			host.status.activeInterface.illustration=source;
		}
	}
	actuators.push(func);
}
OS_Actuator.list.put("illustration",dowithIllustration);

//成员加入脚本
var dowithAppend=function(host,active,node,actuators)
{
	var memberName=node.getAttribute("name");
	var func=function()
	{
		host.status.team.append(memberName);
		var params={
			position:"center",
			width:160,
			height:90,
			words:[memberName+" 加入队伍！"],
			callback:function(){
				active.resume();
				host.status.activeInterface.resume();
			}
		};
		var dialog=new OS_TalkDialog(host,host.status.activeInterface,host.status.activeInterface.getWinRes(),params);
		dialog.open();
		host.status.activeInterface.pause();
		active.pause();
	}
	actuators.push(func);
}
OS_Actuator.list.put("append",dowithAppend);

//成员离开脚本
var dowithLeave=function(host,active,node,actuators)
{
	var memberName=node.getAttribute("name");
	var func=function()
	{
		host.status.team.leave(memberName);
		var params={
			position:"center",
			width:140,
			height:80,
			words:[memberName+" 离开队伍！"],
			callback:function(){
				active.resume();
				host.status.activeInterface.resume();
			}
		};
		var dialog=new OS_TalkDialog(host,host.status.activeInterface,host.status.activeInterface.getWinRes(),params);
		dialog.open();
		host.status.activeInterface.pause();
		active.pause();
	}
	actuators.push(func);
}
OS_Actuator.list.put("leave",dowithLeave);

//游戏结束脚本
var dowithGameover=function(host,active,node,actuators)
{
	var func=function()
	{
		host.loopEndHooks.push(function(){
			host.changeInterface(host.config.entryInterface,"menu");
		});
	}
	actuators.push(func);
}
OS_Actuator.list.put("gameOver",dowithGameover);
